<template>
  <div class="app-container static-container">
    <div class="content-wrap">
      <el-form
        :model="queryParams"
        size="mini"
        ref="queryForm"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="作者" prop="author" label-width="40px">
          <el-input
            v-model="queryParams.author"
            placeholder="请输入作者"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
            style="width: 180px"
          />
        </el-form-item>
        <el-form-item label="发布时间">
          <el-date-picker
            align="right"
            v-model="dateRange"
            size="small"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-badge
            :value="$store.state.statistics.channelIds.length"
            :max="10"
            class="item"
          >
            <el-button type="primary" size="mini" @click="openChannel1"
              >选择栏目</el-button
            >
          </el-badge>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
          <el-button
            icon="el-icon-back"
            size="mini"
            @click="goback"
            v-if="false"
            >返回</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-table v-loading="loading" :data="statisticsList">
      <el-table-column label="序号" type="index" width="55" align="center" />
      <el-table-column label="作者" align="center" prop="author" width="240" />
      <el-table-column label="投稿数量" align="center" prop="monthCount">
        <template slot="header" slot-scope="scope">
          {{ queryParams.startTime }} - {{ queryParams.endTime }}投稿数量
        </template>
      </el-table-column>
      <el-table-column
        label="总投稿数量"
        align="center"
        prop="totalCount"
        width="100"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleQueryAuthor(scope.row)"
            >查看列表</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getChannelListForTree } from "@/api/content/clounm";
import {
  countNumByAuthor, //07、按文章作者统计-汇总
} from "@/api/statistics/statistics";
import { mapState } from "vuex";
export default {
  name: "article_num_channel",
  data() {
    return {
      pickerOptions: {
        shortcuts: [
          {
            text: "本月",
            onClick(picker) {
              const start = new Date();
              start.setDate(1);

              picker.$emit("pick", [start, new Date()]);
            },
          },
          {
            text: "今年至今",
            onClick(picker) {
              const end = new Date();
              const start = new Date(new Date().getFullYear(), 0);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近六个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setMonth(start.getMonth() - 6);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
        disabledDate(time) {
          //time = new Date()
          return time.getTime() > Date.now(); // 可选历史天、可选当前天、不可选未来天
          // return time.getTime() > Date.now() - 8.64e7;  // 可选历史天、不可选当前天、不可选未来天
          // return time.getTime() < Date.now() - 8.64e7;  // 不可选历史天、可选当前天、可选未来天
          // return time.getTime() < Date.now(); // 不可选历史天、不可选当前天、可选未来天
        },
      },
      selectAll: false,
      loading: false,
      menuCheckStrictly: false,
      //全部展开 全部关闭
      value7: false,
      // 统计出的结果
      statisticsList: [],
      total: 0,
      channelIdsTotal: 0,

      channelList: [],

      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
    };
  },

  components: {},

  computed: {
    ...mapState({
      siteRow: (state) => state.column.siteRow,
      queryVuex: (state) => state.statistics.query,
      dateRangeVuex: (state) => state.statistics.dateRange,
      channelIdsVuex: (state) => state.statistics.channelIds,
    }),
  },

  mounted() {
    this.dateRange = this.dateRangeVuex;
    this.queryParams.siteId = this.siteRow.siteId;
    this.getList();
  },

  watch: {},

  methods: {
    openChannel1() {
      this.$emit("openChannel");
    },
    // 获取统计列表
    getList() {
      this.loading = true;
      this.queryParams = {
        ...this.queryParams,
        ...this.queryVuex,
        channelIds: this.channelIdsVuex.join(","),
      };
      countNumByAuthor(
        this.addDateRange(this.queryParams, this.dateRange)
      ).then((res) => {
        this.statisticsList = res.data.list;
        this.total = res.data.totalCount;
        this.loading = false;
      });
    },
    handleQueryAuthor(row) {
      this.$store.dispatch("statistics/setStaticsQuery", {
        author: row.author,
      });
      this.$store.dispatch("statistics/setDateRange", this.dateRange);

      this.$emit("changeActiceName", "article");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.$store.dispatch("statistics/setStaticsQuery", {
        author: this.queryParams.author,
      });

      this.$store.dispatch("statistics/setDateRange", this.dateRange);

      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.$store.dispatch("statistics/setStaticsQuery", {});
      this.handleQuery();
    },
  },
};
</script>

<style scoped lang="scss">
.bottom {
  display: flex;
  // height: 500px;
  height: 50vh;
  .bottom_l {
    // overflow: auto;
    flex: 1;
    height: 100%;
    // background: #fffbf0 !important;
    display: flex;
    flex-direction: column;
    .left-table {
      flex: 1;
      height: 0;
      overflow: overlay;
      overflow: auto;
    }
  }
}
</style>
